package it.webrules.deploymanager.core.dy.deploy.tomcat;

import it.webrules.deploymanager.core.dy.deploy.DeployException;

import java.rmi.ConnectException;

import javax.management.Notification;
import javax.management.NotificationListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReconnectionListener implements NotificationListener {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private final TomcatConnection tomcatConnection;

	public ReconnectionListener(TomcatConnection tomcatConnection) {
		this.tomcatConnection = tomcatConnection;
	}

	@Override
	public void handleNotification(Notification notification, Object jmxConnector) {

		logger.info(notification.toString());

		if (notification.getUserData() instanceof ConnectException) {
			try {
				tomcatConnection.connect();
			} catch (DeployException e) {
				logger.error("Reconnection failed!", e);
			}
		}

	}

}
